import {ajax,getUserInfo} from '@/common/common.js'
import {bpmUrl} from "@/common/config.js"
//初始赋值
function  formonload(list,formData,bussiness){
	let currentNodeid = bussiness.currentNodeid
	let arr = list.map(item => {
		
		return item
	})
	return arr
}

//给相关字段赋值
function fz(obj,item,formData,list){
	formData.cCusName = obj.cCusName
	formData.cCusCode = obj.cCusCode
	formData.comCode1 = obj.comCode1
	let user = getUserInfo()
	let params = {
		paramasMap: {
			cCusCode: obj.cCusCode,
			comCode: ""
		},
		headersMap: {
			sysid: "bpm",
			syspwd: "pass",
			userid: user.bpmUid
		},
		url: bpmUrl + '/rest/appform/getDepartmentByCondition'
	}
	// // 省份
	ajax({
		url:'/bpm/get',
		method: 'POST',
		data: params,
		success:res => {
			formData.Department = res.data.data.Department
		}
	})
	// 客户类别
	params.url = bpmUrl + '/rest/appform/getcCusDefine6'
	ajax({
		url:'/bpm/get',
		method: 'POST',
		data: params,
		success:res => {
			formData.cCusDefine6 = res.data.data.cCusDefine6
		}
	})
	//传真号码(cCusFax)
	params.url = bpmUrl + '/rest/appform/getcCusFax'
	ajax({
		url:'/bpm/get',
		method: 'POST',
		data: params,
		success:res => {
			formData.cCusFax = res.data.data.cCusFax
		}
	})
	//电话(cCusPhone)
	params.url = bpmUrl + '/rest/appform/getcCusPhone'
	ajax({
		url:'/bpm/get',
		method: 'POST',
		data: params,
		success:res => {
			formData.cCusPhone = res.data.data.cCusPhone
		}
	})
	//手机(cCusHand)
	params.url = bpmUrl + '/rest/appform/getcCusHand'
	ajax({
		url:'/bpm/get',
		method: 'POST',
		data: params,
		success:res => {
			formData.cCusHand = res.data.data.cCusHand
		}
	})
	//客户类型(Cust_TYPE)
	params.url = bpmUrl + '/rest/appform/getcCusHand'
	ajax({
		url:'/bpm/get',
		method: 'POST',
		data: params,
		success:res => {
			formData.cCusHand = res.data.data.cCusHand
		}
	})
	// formData.Cust_TYPE = '客户类型'
	//开票类型(cCusDefine2)
	params.url = bpmUrl + '/rest/appform/getcCusDefine2'
	ajax({
		url:'/bpm/get',
		method: 'POST',
		data: params,
		success:res => {
			formData.cCusDefine2 = res.data.data.cCusDefine2
		}
	})
	//合作情况(ccdefine7)
	params.url = bpmUrl + '/rest/appform/getccdefine7'
	ajax({
		url:'/bpm/get',
		method: 'POST',
		data: params,
		success:res => {
			formData.ccdefine7 = res.data.data.ccdefine7
		}
	})
	
	//根据被授权医院名称返回其所对应的大区
	params.url = bpmUrl + '/rest/appform/CRMVR/R_CRMVR_B105'
	ajax({
		url:'/bpm/get',
		method: 'POST',
		data: {
			paramasMap:{
				cCusCode:formData.cCusCode,
				FAE:formData.FAE
			}
		},
		success:res => {
			formData.FAE = res.data.data.FAE
		}
	})
	
	list.map(item => {
		if(['comCode1',
			'cCusCode',
			'Department',
			'cCusFax',
			'cCusPhone',
			'cCusHand',
			'cCusDefine2',
			'ccdefine7',
			'FAE'
			].indexOf(item.name) != -1){
			item.disabled = true // 设置字段disabled
		}
		return item
	})
}

function formonsubmit(list,formData,bussiness){
	let flag = true
	let currentNodeid = bussiness.currentNodeid
	if(currentNodeid == "T10002"){
		//验证客户编码是否存在
		var cCusCode = formData.cCusCode;
		if(cCusCode==""){
			uni.showToast({
				title: "【请填写客户编码！】",
				icon:'none'
			})
			return false;
		}
		//验证客户编码是否存在
		var cCusName = formData.cCusName;
		if(cCusName==0){
			uni.showToast({
				title:"【客户名称和编码有误，请检查后重新输入！】",
				icon:'none'
			})
			return false;
		}         
		//验证数量字段不能为空
		var docunid = bussiness.docUnid;
		let user = getUserInfo()
		let paramas = {
			paramasMap: {
				docunid:docunid
			},
			headersMap: {
				sysid: "bpm",
				syspwd: "pass",
				userid: user.bpmUid
			},
			url: bpmUrl + '/rest/appform/CRMVR/R_CRMVR_B120'
		}
		ajax({
			url:'/bpm/get',
			method: 'POST',
			data: paramas,
			success:res => {
				if(res.data.data= 1){
				//alert("【请填写表体中数量！】");
				//return false;
				}
			}
		})
		//验证子表信息不为空
		paramas.url = bpmUrl + '/rest/appform/CRMVR/R_CRMVR_B102'
		ajax({
			url:'/bpm/get',
			method: 'POST',
			data: paramas,
			success:res => {
				if(res.data.data= 1){
				//alert("【请填写表体中数量！】");
				//return false;
				}
			}
		})
		
		//验证预算名称不能为空
		let BudgetCode = formData.BudgetCode
		if(BudgetCode==""){
			uni.showToast({
				title:"请选择预算名称",
				icon:'none'
			})
			return false;
		}
		
		var Use_App = formData.Use_App
		if(Use_App==""){
			uni.showToast({
				title:"【请填写申请用途！】",
				icon:'none'
			})
		    return false;
		}
		
	}
	list = list.map(item => {
		if(['cCusCode','comCode1','Department','cCusDefine6','cCusFax',
		'cCusPhone','cCusHand','Cust_TYPE','cCusDefine7','cCusDefine2',
		'FAE'].indexOf(item.name) != -1){
			item.disabled = false
		}
		return item
	})
	return flag;
}

//模糊搜索客户列表
function getCustomerName(queryString,formData, cb) {
	console.log(queryString, cb)
	let user = getUserInfo()
	let paramas = {
		paramasMap: {
			cCusName: queryString
		},
		headersMap: {
			sysid: "bpm",
			syspwd: "pass",
			userid: user.bpmUid
		},
		url: bpmUrl + '/rest/appform/getCustomerInfo'
	}
	ajax({
		url: '/bpm/get',
		method: 'POST',
		data: paramas,
		success: res => {
			console.log(res)
			let data = res.data.data.map(item => {
				item.id = item.cCusCode
				item.name = item.cCusName
				return item
			})
			console.log(data)
			cb(data)
		}
	})
}


export default {
	formonload,
	fz,
	formonsubmit,
	getCustomerName
}